#!/usr/bin/env python
#-*- coding:utf-8 -*-

import os
import sys
import string
import socket
import ConfigParser

#read config                                                                
cf = ConfigParser.ConfigParser()
cf.read(os.path.dirname(os.path.realpath(__file__))+"/settings.conf")
ip = cf.get("SMS", "ip")
port = cf.getint("SMS", "port")

def sendsms(*arg):
    length = len(arg)
    count = 0
    str = "m:"
    for x in arg:
        count += 1
        if count < length-1:
            str += x + ","
        elif count < length:
            str += x + " "
        else:
            str += "c:" + x
    length = len(str)
    smsStr = "s:%04d %s" % (length, str)
    print '''-----------------------------------------
Send: %s
-----------------------------------------''' % (smsStr)

    #UDP
    client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    client.sendto(smsStr, (ip, port)) 
    client.close()

def sendsms2(mobile, message):
    str = "m:%s c:%s" % (mobile, message)
    length = len(str)
    smsStr = "s:%04d %s" % (length, str)
    print '''-----------------------------------------
Send: %s
-----------------------------------------''' % (smsStr)

    #UDP
    client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    client.sendto(smsStr, (ip, port)) 
    client.close()

def help():
    print '''
[usage]
1. 命令行(两个字符串参数)
    shell中: ./sendMessage.py "131***,132***,133***" "hello sms shell"
    python中: python sendMessage.py 131***,132***,133*** "hello sms shell"
2. python函数(任意个数的“可变参数”)
    sendsms("131***", "189***", "hello sms function")
'''

if __name__ == '__main__':
    if len(sys.argv) != 3:
        help()
        #sendsms("13161509416", "18910056897", "hello sms function")
    else:
        sendsms2(sys.argv[1], sys.argv[2])



 
